﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Iesi.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class SchoolRoleGroupAdmin
    {
        #region AddSchoolRoleGroup添加一个学校权限组
        /// <summary>
        /// 添加一个学校权限组
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="roleGroupName"></param>
        /// <param name="roleIds"></param>
        public static void AddSchoolRoleGroup(int schoolId,string roleGroupName, string roleIds)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                string[] roleIdArray = Tools.DeleteStringArrayRepeat(roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                IList<Basic.Model.Entity.Collision> collisionList = new List<Basic.Model.Entity.Collision>();
                if (String.IsNullOrEmpty(roleGroupName))
                    throw new Exception("权限组名称不能为空");
                if (roleIdArray.Length == 0)
                    throw new Exception("你必须选择至少一个以上权限");
                //冲突处理
                for (int i = 0; i < roleIdArray.Length; i++)
                {
                    Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                    collision.itemKey = "DeleteSchoolRole" + roleIdArray[i];
                    Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                    collisionList.Add(collision);
                }
                SchoolRoleGroup schoolRoleGroup = new SchoolRoleGroup();
                schoolRoleGroup.school = SchoolManager.Get(session, schoolId);
                if (schoolRoleGroup.school == null)
                    throw new Exception("不存在这个学校或者这个学校已经被删除！");
                schoolRoleGroup.roleGroupName = roleGroupName;
                schoolRoleGroup.schoolRoles = new HashedSet<SchoolRole>();
                for (int i = 0; i < roleIdArray.Length; i++)
                {
                    SchoolRole schoolRole = SchoolRoleManager.Get(session, int.Parse(roleIdArray[i]));
                    if (schoolRole != null)
                        schoolRoleGroup.schoolRoles.Add(schoolRole);
                }
                SchoolRoleGroupManager.Add(session, schoolRoleGroup);

                //删除冲突
                for (int i = 0; i < collisionList.Count; i++)
                {
                    Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collisionList[i]);
                }
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region UpdateSchoolRoleGroup更新一个学校权限组
        /// <summary>
        /// 更新一个学校权限组
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="roleGroupId"></param>
        /// <param name="roleGroupName"></param>
        /// <param name="roleIds"></param>
        public static void UpdateSchoolRoleGroup(int schoolId, int roleGroupId, string roleGroupName, string roleIds)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                string[] roleIdArray = Tools.DeleteStringArrayRepeat(roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                IList<Basic.Model.Entity.Collision> collisionList = new List<Basic.Model.Entity.Collision>();
                if (String.IsNullOrEmpty(roleGroupName))
                    throw new Exception("权限组名称不能为空");
                if (roleIdArray.Length == 0)
                    throw new Exception("你必须选择至少一个以上权限");
                //冲突处理
                for (int i = 0; i < roleIdArray.Length; i++)
                {
                    Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                    collision.itemKey = "DeleteSchoolRole" + roleIdArray[i];
                    Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                    collisionList.Add(collision);
                }
                SchoolRoleGroup schoolRoleGroup = SchoolRoleGroupManager.Get1(session,schoolId,roleGroupId);
                if (schoolRoleGroup == null)
                    throw new Exception("不存在这个权限组或者这个权限组已经被删除！");
                schoolRoleGroup.roleGroupName = roleGroupName;
                schoolRoleGroup.schoolRoles = new HashedSet<SchoolRole>();
                for (int i = 0; i < roleIdArray.Length; i++)
                {
                    SchoolRole schoolRole = SchoolRoleManager.Get(session, int.Parse(roleIdArray[i]));
                    if (schoolRole != null)
                        schoolRoleGroup.schoolRoles.Add(schoolRole);
                }

                //删除冲突
                for (int i = 0; i < collisionList.Count; i++)
                {
                    Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collisionList[i]);
                }
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region DeleteSchoolRoleGroup 删除一个权限组
        /// <summary>
        /// 删除一个权限组
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="roleGroupId"></param>
        public static void DeleteSchoolRoleGroup(int schoolId,int roleGroupId)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteSchoolRoleGroup" + roleGroupId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);

                SchoolRoleGroup schoolRoleGroup = SchoolRoleGroupManager.Get2(session, schoolId, roleGroupId);
                if (schoolRoleGroup == null)
                    throw new Exception("这个权限组不存在或者已经被删除！");
                if (schoolRoleGroup.positions.Count > 0)
                    throw new Exception("当前学校有职位与本权限组关联，因此不能删除！请先移除职位关联！");
                SchoolRoleGroupManager.Delete(session, schoolRoleGroup);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region GetsSchoolRoleGroup 查找所有权限组
        /// <summary>
        /// 查找所有权限组
        /// </summary>
        /// <param name="schoolId"></param>
        /// <returns></returns>
        public static IList<Educational.VO.SchoolRoleGroup> GetsSchoolRoleGroup(int schoolId)
        { 
            ISession session = null;
            ITransaction trans = null;
            IList<SchoolRoleGroup> entityData = null;
            IList<Educational.VO.SchoolRoleGroup> result = new List<Educational.VO.SchoolRoleGroup>();
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = SchoolRoleGroupManager.Gets1(session, schoolId);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            { 
                Educational.VO.SchoolRoleGroup schoolRoleGroup=new Educational.VO.SchoolRoleGroup();
                schoolRoleGroup.roleGroupId = entityData[i].roleGroupId;
                schoolRoleGroup.roleGroupName = entityData[i].roleGroupName;
                schoolRoleGroup.roles = new List<Educational.VO.SchoolRole>();
                IEnumerator<SchoolRole> schoolRoles = entityData[i].schoolRoles.GetEnumerator();
                while (schoolRoles.MoveNext())
                {
                    Educational.VO.SchoolRole schoolRole = new Educational.VO.SchoolRole();
                    schoolRole.roleId = schoolRoles.Current.roleId;
                    schoolRole.roleName = schoolRoles.Current.roleName;
                    schoolRoleGroup.roles.Add(schoolRole);
                }
                result.Add(schoolRoleGroup);
            }
            return result;
        }
        #endregion
    }
}
